package com.cuit.blog.controller;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.cuit.blog.entity.Article;
import com.cuit.blog.entity.Articletype;
import com.cuit.blog.entity.User;
import com.cuit.blog.service.ArticleService;
import com.cuit.blog.service.ArticletypeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
@Controller
@RequestMapping("/article")
public class ArticleController {
	@Autowired
	ArticleService articleService;
	@Autowired
	ArticletypeService articletypeService;
	int pageSize = 10;
	@GetMapping("/findAll")
	public String findAll(Model model) {
		Page<Article> page = articleService.findAllByPage(0, pageSize);
		int pre = page.previousOrFirstPageable().getPageNumber();
		int next = page.nextOrLastPageable().getPageNumber();
		model.addAttribute("pre", pre);
		model.addAttribute("next", next);
		model.addAttribute("page", page);
		return "article/list";
	}
	@GetMapping("/page")
	public String findByPage(@RequestParam("pageIndex") int pageIndex, Model model) {
		Page<Article> articlePage = articleService.findAllByPage(pageIndex, pageSize);
		int pre = articlePage.previousOrFirstPageable().getPageNumber();
		int next = articlePage.nextOrLastPageable().getPageNumber();
		model.addAttribute("pre", pre);
		model.addAttribute("next", next);
		model.addAttribute("page", articlePage);
		return "article/list";
	}
	@GetMapping("/toAdd")
	public String add(Model model) {
		List<Articletype> typeList = articletypeService.findAll();
		model.addAttribute("typeList", typeList);
		return "article/add";
	}
	@GetMapping("/toEdit")
	public String getOne(@RequestParam("id") Long id, Model model) {
		Article article = articleService.findById(id);
		List<Articletype> type = articletypeService.findAll();
		model.addAttribute("article", article);
		model.addAttribute("typeList", type);
		return "article/add";
	}
	@GetMapping("/show")
	public String show(@RequestParam("id") Long id, Model model) {
		Article article = articleService.findById(id);
		List<Articletype> type = articletypeService.findAll();
		model.addAttribute("article", article);
		model.addAttribute("typeList", type);
		return "article/show";
	}
	@PostMapping("/save")
	public String save(HttpServletRequest request, @ModelAttribute("article") Article article,
			@RequestParam("typeId") Long typeId) {
		Articletype articleType = new Articletype();
		articleType.setId(typeId);
		User user;
		HttpSession session = request.getSession();
		if (session.getAttribute("LoginUser") != null) {
			user = (User) session.getAttribute("LoginUser");
		} else {
			user = new User();
			user.setId(1);
		}
		article.setUser(user);
		article.setArticletype(articleType);
		article.setPubDate(new Date());
		articleService.save(article);
		return "redirect:/article/findAll";
	}
	@GetMapping(path = "/delete")
	public String delete(@RequestParam long id) {
		articleService.deleteById(id);
		return "redirect:/article/findAll";
	}
}
